﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HK.Core.Extend;
using HK.Editor.Common;
using HK.Editor.GUIEx.Styles;
using HK.Editor.Utils;
using UnityEditor;
using UnityEngine;

namespace HK.Editor.GUIEx.Helper
{
    public class ColorInfo
    {
        public string ColorName = null;
        public Color Color = Color.clear;
    }
    
    public sealed class GUIColorsViewer : EditorWindow
    {
        
        private static GUIStyle _btnStyleNormal = null;
        protected static GUIStyle BtnStyleNormal 
        {
            get
            {
                if (null != _btnStyleNormal) return _btnStyleNormal;
                _btnStyleNormal = GuiExHelper.CloneStyle("EditModeSingleButton");
                _btnStyleNormal.alignment = TextAnchor.MiddleLeft;

                _btnStyleNormal.padding.left += 5;
                _btnStyleNormal.richText = true;
                _btnStyleNormal.wordWrap = true;
                _btnStyleNormal.padding.right += 5;

                return _btnStyleNormal;
            }
        }

#if UGUIEX_HELPER_ENABLE
        [MenuItem(EditorConst.MENU_TXT_GUI_COLORS_VIEW, false, EditorConst.MENU_PRIORITY_GUI_COLORS_VIEW)]
#endif
        private static void OpenGUIIcon()
        {
            var wnd = GetWindow<GUIColorsViewer>("颜色预览表");
            wnd.minSize = new Vector2(840, 480);
            wnd.maxSize = new Vector2(1080, 720);
            
            wnd.Show();
        }

        private Vector2 scrollPosition;
        private string _searchKey = "";
        private const float width = 180f;

        private List<ColorInfo> ViewColors = null;

        private void OnGUI()
        {
            if (null == ViewColors)
            {
                ViewColors = new List<ColorInfo>();
                var colorInfos = typeof(ColorEx).GetFields(BindingFlags.Public | BindingFlags.Static);
                var count = colorInfos.Length;
                for (var idx = 0; idx < count; ++idx)
                {
                    var colorInfo = colorInfos[idx];

                    var info = new ColorInfo
                    {
                        ColorName = colorInfo.Name, 
                        Color = (Color)colorInfo.GetValue(colorInfo)
                    };
                    ViewColors.Add(info);
                }
            }
            
            
            GUILayout.BeginHorizontal("Toolbar");
            {
                GUILayout.Label($"Search({ViewColors.Count}):", GUILayout.Width(120));
                _searchKey = GUILayout.TextField(_searchKey, "SearchTextField");
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(GuiExHelper.GroupBox);
            scrollPosition = GUILayout.BeginScrollView(scrollPosition);
            {
                var widthTmp = width; 
                var count = Mathf.FloorToInt((position.width - 10) / (widthTmp + 3f));
                var curColors = ViewColors.Where(iO => iO.ColorName.ToLower().Contains(_searchKey.ToLower())).ToList();
                for (var i = 0; i < curColors.Count; i += count)
                {
                    GUILayout.BeginHorizontal();
                    for (var j = 0; j < count; j++)
                    {
                        var index = i + j;
                        if (index < curColors.Count)
                        {
                            var curColor = curColors[index];
                            GUILayout.BeginHorizontal(GuiExHelper.GroupBox);
                            using (new BackgroundColorScope(curColor.Color))
                            {
                                GUILayout.Label(" ", GuiExHelper.GroupBox, GUILayout.Width(20));
                            }
                            GUILayout.Label(curColor.ColorName, GuiExHelper.TitleLeft, GUILayout.Width(widthTmp - 20));
                            GUILayout.EndHorizontal();
                    
                            var lastRect = GUILayoutUtility.GetLastRect();
                            if (Event.current.type == EventType.ContextClick  && lastRect.Contains(Event.current.mousePosition))
                            {
                                ShowDependMenus(curColor);
                                Event.current.Use();
                            }
                        }
                    }
                    GUILayout.EndHorizontal();
                }
            }
            GUILayout.EndScrollView();
            GUILayout.EndHorizontal();
        }
        
        /// <summary>
        /// 显示依赖目标菜单
        /// </summary>
        /// <param name="iCurColor">当前依赖</param>
        private void ShowDependMenus(ColorInfo iCurColor)
        {
            if(null == iCurColor) return;
            
            var menu = new GenericMenu();
            menu.AddItem(new GUIContent("拷贝"), false, CopyColorName, iCurColor);
            menu.ShowAsContext();
        }

        /// <summary>
        /// 导出Icon
        /// </summary>
        /// <param name="iProperty">Icon Property</param>
        private void CopyColorName(object iProperty)
        {
            var colorInfo = iProperty as ColorInfo;
            if(null == colorInfo) return;
            
            UtilsGUIEx.CopyToClipBoard($"ColorEx.{colorInfo.ColorName}");
            this.Info("<color=\"{0}\">CopyColorName():{1} - {2}</color>", 
                ColorEx.ConvertColorToHexStr(colorInfo.Color), colorInfo.ColorName, colorInfo.Color);
        }
    }
}